package com.jianz.campus_markets.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jianz.campus_markets.domain.entity.R;
import com.jianz.campus_markets.domain.entity.SensitiveWord;
import com.jianz.campus_markets.domain.query.PageParam;
import com.jianz.campus_markets.filter.SensitiveFilter;
import com.jianz.campus_markets.mapper.SensitiveWordMapper;
import com.jianz.campus_markets.service.SensitiveWordService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * {@code @Author}: lzy.
 * {@code @Date}: Created in 2023/5/17
 * {@code @Project}: Information-sharing
 * {@code @Description}: 描述
 */
@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {
    @Autowired
    SensitiveWordMapper sensitiveWordMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    SensitiveFilter sensitiveFilter;
    @Override
    public R addSensitiveWord(SensitiveWord sensitiveWord) {
//        String username = SecurityUtils.getUser().getUsername();
//        sensitiveWord.setFounder(username);
        sensitiveWordMapper.insertSelective(sensitiveWord);
//        redisTemplate.convertAndSend("usw", "通知");
        sensitiveFilter.init();
        return R.ok().message("添加成功！");
    }

    @Override
    public R deleteSensitiveWord(List<Long> id) {
        sensitiveWordMapper.deleteByPrimaryKey(id);
        sensitiveFilter.init();
//        redisTemplate.convertAndSend("usw", "通知");
        return R.ok().message("删除成功！");
    }

    @Override
    public R updateSensitiveWord(SensitiveWord sensitiveWord) {
        sensitiveWordMapper.updateByPrimaryKeySelective(sensitiveWord);
        sensitiveFilter.init();
//        redisTemplate.convertAndSend("usw", "通知");
        return R.ok().message("修改成功！");
    }

    @Override
    public R getSensitiveWord(PageParam param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<SensitiveWord> sensitiveWords = sensitiveWordMapper.selectPage();
        PageInfo<SensitiveWord> page = new PageInfo<>(sensitiveWords,5);
        return R.ok().message("获取成功！")
                .data("sensitiveWords",page.getList())
                .data("navigatepageNums",page.getNavigatepageNums())
                .data("hasPreviousPage",page.isHasPreviousPage())
                .data("hasNextPage",page.isHasNextPage());
    }

    @Override
    public R isSensitive(String text) {
        boolean sensitive = sensitiveFilter.isSensitive(text);
        return R.ok().message("判断成功！").data("isSensitive",sensitive);
    }


}
